group by using reduce function